/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package myheartsgame;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author KaKa
 */
public class Card extends JButton{

    // Chat^' va so' nuoc'
    private int _suit;
    private int _number;
    
    // Mot la bai khi o 1 nguoi nhin thi chi co trang thai mo --> luu 1 hinh
    private ImageIcon _cardIcon;
    private ImageIcon _closeCardIcon;
    private boolean _canSelected;
    private boolean _isSelected;
    private boolean _isOpen;
    // Kiem tra co phai dang di chuyen hay khong
    public boolean _isMoving;
    public boolean _isOpening;

    /* Kich thuoc la bai */
    public int _Width;
    public int _Height;

    /* Toc do la bai */
    public int _speed;

    // Khoi tao bai
    public void InitCard() {

        // Ten file trong resource co dang number_suit
        // VD: 7 bich <=> 7_0
        try {
            String nameImageInResource = "/Pictures/Card/" + Integer.toString(_number) + "_" + Integer.toString(_suit) + ".gif";
            _cardIcon = new ImageIcon(getClass().getResource(nameImageInResource));
            String nameCloseCardImage = "/Pictures/Card/0_0" + ".gif";
            _closeCardIcon = new ImageIcon(getClass().getResource(nameCloseCardImage));

            _canSelected = true;
            _isSelected = false;
            _isOpen = false;
            _isMoving = false;
            _isOpening = false;
            _speed = 20;

            setIcon(_closeCardIcon);
            _Width = _cardIcon.getImage().getWidth(null);
            _Height = _cardIcon.getImage().getHeight(null);
            setSize(_Width + 2, _Height + 2);

            setContentAreaFilled(false);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Can not find resources.");
            setText("Can not find image!");
            return;
        }
    }

    // Khoi tao la bai voi suit va number
    public Card(int suit, int number){

        if(number < 2 || number > CardsInfo.A)
            number = 2;
        
        _suit = suit;
        _number = number;
        InitCard();
        AddEvent();
    }

    // Khoi tao la bai tu info
    public Card(String info) {
        AnalyzeInfo(info);
        InitCard();
        AddEvent();
    }

    // Them su kien click tren la bai
    public void AddEvent() {
        addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {                
                if(!_canSelected)
                    return;
                /*
                _isSelected = !_isSelected;
                int y = getY();
                if (_isSelected) {
                    y -= 30;
                } else {
                   y += 30;
                }

                setLocation(getX(), y);*/
                _isMoving = true;
                SetDestinationMove(GlobalClass.ScreenWidth / 2 + _Width, GlobalClass.ScreenHeight / 2 - _Height);
            }
        });
    }

    // Lay suit va number trong chuoi info
    public void AnalyzeInfo(String info) {
        String strSuit = info.substring(0, 1);
        String strNumber = info.substring(2);

        try {
            _suit = Integer.parseInt(strSuit);
            _number = Integer.parseInt(strNumber);
        } catch (Exception ex) {
        }
    }

    // Xuat thong tin cua card
    public String GetCardInfo() {

        String info = Integer.toString(_suit) + "_" + Integer.toString(_number);
        return info;
    }

    // Gan thong tin info cho card
    public void SetCardInfo(String info) {
        AnalyzeInfo(info);
        String nameImageInResource = "/Pictures/Card/" + Integer.toString(_number) + "_" + Integer.toString(_suit) + ".gif";
        _cardIcon = new ImageIcon(getClass().getResource(nameImageInResource));

    }

    // Lat bai tu up --> ngua
    public void openCard() {
        _isOpen = true;
        setIcon(_cardIcon);
    }

    // Thay doi trang thai selected cua la bai
    public void setSelected(boolean value) {
        _canSelected = value;
    }

    // Lay chat cua la bai
    public int getSuit(){
        return _suit;
    }

    // Lay so thu tu
    public int getNumber() {
        return _number;
    }

    // Kiem tra bai co duoc chon hay khong
    public boolean _isCardSelected(){
        return _isSelected;
    }

    // Bien de luu gia tri dich den de tiep tuc Update
    private Point DesMove = new Point();
    // Di chuyen la bai toi diem dich (dirX, dirY)
    public void SetDestinationMove(int dirX, int dirY) {
        DesMove.x = dirX;
        DesMove.y = dirY;
        _isMoving = true;
    }

    public void Move() {
               
        int x = getX();
        int y = getY();
        int moveX = Math.abs(DesMove.x - x);
        int distanceX = _speed;
        if(moveX < _speed)
            distanceX = moveX;

        if (x < DesMove.x) {
            x += distanceX;
        } else {
            x -= distanceX;
        }

        int moveY = Math.abs(DesMove.y - y);
        int distanceY = _speed;
        if(moveY < _speed)
            distanceY = moveY;

        if (y < DesMove.y) {
            y += distanceY;
        } else {
            y -= distanceY;
        }

        if (x == DesMove.x && y == DesMove.y) {
            _isMoving = false;
        }

        setLocation(x, y);
    }

    // Khi lat bai thi la bai se lui lai mot khoang
    // roi se quay lai vi tri dau
    // _turnBool de thay doi duong di la bai trong ham Update()
    boolean _turnBool = false;
    public void Update() {

        if (_isMoving) {
            Move();
        }

        // Xu ly khi dang lat la bai
        if (_isOpening) {
            if (!_turnBool) {
                setLocation(getX() + 20, getY());
                _turnBool = true;
            } else {
                setLocation(getX() - 20, getY());
                _turnBool = false;
                _isOpening = false;
                openCard();
            }
        }

    }
}
